package com.woniu.elderly.wares.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.common.dto.WaresSpuDTO;
import com.woniu.common.entity.Result;
import com.woniu.common.entity.WaresSpu;
import com.woniu.common.vo.SpuVO;
import com.woniu.elderly.wares.service.WaresSpuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Author: xuan
 * @Date: 2024/8/2 17:57
 * @Version: 1.0
 */
@Controller
@ResponseBody
@Slf4j
@RequestMapping("/wares/spu")
public class WaresSpuController {
    @Autowired
    private WaresSpuService waresSpuService;
    // 这里需要写，根据分类id,查询商品分页查询
    // 根据分类id、每页大小和页码查询商品分页信息
    @GetMapping("/{cid}/{pageSize}/{pageIndex}")
    public Result getWaresSpuByCid(@PathVariable("cid") Integer cid,
                                   @PathVariable("pageSize") Integer pageSize,
                                   @PathVariable("pageIndex") Integer pageIndex) {

        // 创建查询条件包装器
        QueryWrapper<WaresSpu> queryWrapper = new QueryWrapper<>();
        // 设置查询条件：分类id等于传入的cid
        queryWrapper.eq("category_id", cid);

        // 调用waresSpuService的page方法进行分页查询，并传入分页参数和查询条件
        Page page = waresSpuService.page(new Page<>(pageIndex, pageSize), queryWrapper);
        // 打印当前页码
        // 返回查询结果
        return Result.ok(page);
    }
    //通过sid,寻找商品
    @GetMapping("/{sid}")
    public Result getWaresSpuDTO(@PathVariable("sid") Integer sid){
        // 调用waresSpuService的getWaresSpuDTO方法，传入sid
        log.info("sid:{}", sid);
       WaresSpuDTO waresSpuDTO =  waresSpuService.getWaresSpuDTO(sid);
       return Result.ok(waresSpuDTO);
    }
    @GetMapping("/")
    public Result getWaresSpuVO(){
        log.info("getWaresSpuVO");
       List<SpuVO> spuVOList =  waresSpuService.getSpuVO();
       return Result.ok(spuVOList);
    }
}
